<?php

namespace App\Models\Applet\Order;

class ShopOrderModel extends BaseOrderModel
{
    protected $table = 'shop_orders';

    const ORDER_STATUS_WAIT     = 10; // 等待支付
    const ORDER_STATUS_CANCEL   = 20; // 取消支付
    const ORDER_STATUS_SUCCESS  = 30; // 已支付，待发货
    const ORDER_STATUS_DELIVERY = 40; // 已发货

    const ORDER_STATUS_COMPLETED = 60; // 已完成

    /**
     * 查询子订单列表
     * @param array $orderMasterIds
     * @param array $fields
     * @return array
     */
    public function getItemByOrderMasterIds(array $orderMasterIds, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->whereIn('order_master_id', $orderMasterIds)
                    ->where('deleted', self::DELETED_NO)
                    ->get()
                    ->toArray();
    }

    /**
     * 查询子订单
     * @param int $orderMasterId
     * @param array $fields
     * @return array
     */
    public function getItemByOrderMasterId(int $orderMasterId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('order_master_id', $orderMasterId)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据订单号查询数据
     * @param string $orderNo
     * @param array $fields
     * @return array
     */
    public function getItemByOrderNo(string $orderNo, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('order_no', $orderNo)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 取消订单
     * @return void
     */
    public function cancel(int $orderId)
    {
        $this->where('id', $orderId)
             ->where('deleted', self::DELETED_NO)
             ->update(['order_status' => self::ORDER_STATUS_CANCEL]);
    }

    /**
     * 订单总条数
     * @param array $params
     * @return int
     */
    public function total(array $params): int
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(users.nickname like "%' . $params['keyword'] . '%"' .
                        ' or phone like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(shop_orders.id as char) like "%' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }

        if (isset($params['orderStatus'])) {
            $query = $query->where('shop_orders.order_status', $params['orderStatus']);
        }

        return $query->join('shop_order_goods', 'shop_order_goods.order_id', '=', 'shop_orders.id')
                     ->join('shop_master_orders', 'shop_master_orders.id', '=', 'shop_order_goods.order_master_id')
                     ->join('users', 'users.id', '=', 'shop_master_orders.user_id')
                     ->where('shop_orders.deleted', self::DELETED_NO)
                     ->count();
    }

    /**
     * 订单列表
     * @param array $params
     * @param array $fields
     * @return array
     */
    public function list(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this->select($fields)
                      ->join('shop_order_goods', 'shop_order_goods.order_id', '=', 'shop_orders.id')
                      ->join('shop_master_orders', 'shop_master_orders.id', '=', 'shop_order_goods.order_master_id')
                      ->join('users', 'users.id', '=', 'shop_master_orders.user_id')->where('shop_orders.deleted', self::DELETED_NO);

        if (!empty($params['keyword'])) {
            $whereRaw = '(users.nickname like "%' . $params['keyword'] . '%"' .
                        ' or phone like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(shop_orders.id as char) like "%' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }

        if (isset($params['orderStatus'])) {
            $query = $query->where('shop_orders.order_status', $params['orderStatus']);
        }

        return $query
            ->orderBy('id', 'desc')
            ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
            ->limit(PAGE_SIZE)
            ->get()
            ->toArray();
    }

    /**
     * 根据主订单id更新数据
     * @return void
     */
    public function updateItemByOrderMasterId(int $orderMasterId, array $data): bool
    {
        return $this->where('order_master_id', $orderMasterId)
                    ->where('deleted', self::DELETED_NO)
                    ->update($data);
    }
}
